package com.example.zyfypt516wh.activities;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;


import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.bean.CollectResult;
import com.example.zyfypt516wh.bean.FocusResult;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.common.Common;
import com.example.zyfypt516wh.iface.CollectionListener;
import com.example.zyfypt516wh.iface.DetailListener;
import com.example.zyfypt516wh.iface.FocusListener;
import com.example.zyfypt516wh.model.CollectionModel;
import com.example.zyfypt516wh.model.DetailModel;
import com.example.zyfypt516wh.model.FocusModel;
import com.example.zyfypt516wh.utils.FileUtil;

import java.util.List;

import es.voghdev.pdfviewpager.library.RemotePDFViewPager;
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter;
import es.voghdev.pdfviewpager.library.remote.DownloadFile;


public class PDFWebViewActivity extends Activity implements DownloadFile.Listener {

    private String url;//接收URL

    private TextView titlePdf;//title

    private LinearLayout root;
    RemotePDFViewPager remotePDFViewPager;
    PDFPagerAdapter adapter;

    private SharedPreferences sp;
    String id, type, sessionID;
    RelativeLayout view;
    private TextView tvCollect;
    private TextView tvFollow;
    String focusType = "userfocus";

    String userid, collecStatus, followStatus;

    private DetailListener listener = new DetailListener() {
        @Override
        public void onResponse(ResBean bean) {
            url = Common.IMAGEURL + "/" + bean.getPdfattach();
            titlePdf.setText(bean.getName());
            remotePDFViewPager = new RemotePDFViewPager(PDFWebViewActivity.this, url, PDFWebViewActivity.this);
            remotePDFViewPager.setId(R.id.pdfViewPager);

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(PDFWebViewActivity.this, "失败： " + msg, Toast.LENGTH_SHORT).show();
            System.out.println("--" + msg);
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences("user", MODE_PRIVATE);

        type = getIntent().getStringExtra("type");
        id = getIntent().getStringExtra("id");

        userid = getIntent().getStringExtra("userid");

        sessionID = sp.getString("sessionID", "");
        DetailModel model = new DetailModel();
        model.getDetail("tware", id, sessionID, listener);

        setContentView(R.layout.activity_pdf);

        titlePdf = findViewById(R.id.pdf_title);
        root = findViewById(R.id.pdf_page_container);
        view = findViewById(R.id.pdf_head);
        tvCollect = findViewById(R.id.title_collection);
        tvFollow = findViewById(R.id.title_follow);
//        tvCollect.setText("收藏");
//        tvFollow.setText("关注");

        //判断是否收藏
        existCollection();
        //判断是否关注
        existFollow();

        //收藏
        tvCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (collecStatus.equals("1")){//状态1 尚未收藏
                    toCollection();
                }else if (collecStatus.equals("0")){//状态0 已收藏--》取消收藏
                    unCollection();
                }


            }
        });
        //关注
        tvFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (followStatus.equals("1")){//状态1 未关注
                    toFollow();
                }else if (followStatus.equals("0")){//状态0 已关注--》取消关注
                    unFollow();
                }

            }
        });

    }


    FocusListener existFListener =new FocusListener() {
        @Override
        public void onResponse(List<FocusResult> list) {

        }

        @Override
        public void onResponse(String res) {
            followStatus = res;
            if (followStatus.equals("1")) {
                tvFollow.setText("关注");
            } else if (followStatus.equals("0")) {
                tvFollow.setText("取消关注");
            }
        }

        @Override
        public void onFail(String msg) {

        }

    };

    /**
     * 判断是否关注
     */
    private void existFollow() {
        FocusModel model=new FocusModel();
        model.exist(focusType,Integer.parseInt(userid),sessionID,existFListener);
    }


    FocusListener focusListener =new FocusListener() {
        @Override
        public void onResponse(List<FocusResult> list) {

        }

        @Override
        public void onResponse(String res) {
            Log.i("TAG", "关注结果onResponse:---》 "+res);
            existFollow();

        }

        @Override
        public void onFail(String msg) {
            System.out.println("判断是否关注请求--->"+msg);
        }
    };

    /**
     * 关注
     */
    private void toFollow() {
        FocusModel model=new FocusModel();
        model.focus(focusType,Integer.parseInt(userid),sessionID, focusListener);
    }

    /**
     * 取消关注
     */
    private void unFollow() {
        FocusModel model=new FocusModel();
        model.unfocus(focusType,Integer.parseInt(userid),sessionID, focusListener);
    }

    CollectionListener existListener =new CollectionListener() {
        @Override
        public void onResponse(String res) {
            collecStatus=res;
            if (collecStatus.equals("1")){
                tvCollect.setText("收藏");
            }else if (collecStatus.equals("0")){
                tvCollect.setText("取消收藏");
            }
        }
        @Override
        public void onResponse(List<CollectResult> list) { }
        @Override
        public void onFail(String msg) {
            System.out.println("判断是否存在请求--->"+msg);
        }
    };

    //判断是否收藏网络请求
    private void existCollection() {
        CollectionModel model=new CollectionModel();
        model.exist(type,Integer.parseInt(id),sessionID,existListener);
    }

    CollectionListener collectionListener =new CollectionListener() {
        @Override
        public void onResponse(String res) {
            Log.i("TAG", "收藏结果onResponse:---》 "+res);
            existCollection();

        }
        @Override
        public void onResponse(List<CollectResult> list) { }
        @Override
        public void onFail(String msg) {
            System.out.println("判断是否存在请求--->"+msg);
        }
    };
    //收藏网络请求
    private void toCollection() {
        CollectionModel model=new CollectionModel();
        model.collect(type,Integer.parseInt(id),sessionID,collectionListener);
    }

    //取消收藏网络请求
    private void unCollection() {
        CollectionModel model=new CollectionModel();
        model.uncollect(type,Integer.parseInt(id),sessionID,collectionListener);
    }


    public void updateLayout() {
        root.removeAllViewsInLayout();

        root.addView(view, LinearLayout.LayoutParams.MATCH_PARENT, 90);
        root.addView(titlePdf, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        root.addView(remotePDFViewPager, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onSuccess(String url, String destinationPath) {
        adapter = new PDFPagerAdapter(this, FileUtil.extractFileNameFromURL(url));
        remotePDFViewPager.setAdapter(adapter);
//        setContentView(remotePDFViewPager);
        updateLayout();
    }

    @Override
    public void onFailure(Exception e) {
//        Toast.makeText(PDFWebViewActivity.class,"加载失败",Toast.LENGTH_SHORT);
    }

    @Override
    public void onProgressUpdate(int progress, int total) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.close();
        }
    }
}

